package com.mewpipe.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mewpipe.dao.CommentDao;
import com.mewpipe.dao.DaoFactory;
import com.mewpipe.dao.LnkCommentVideoDao;
import com.mewpipe.entity.Comment;
import com.mewpipe.entity.UserSession;


@WebServlet("/AddComment")
public class AddComment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
private CommentDao commentDao;
private LnkCommentVideoDao lnkDao;
	
	@Override
	public void init() throws ServletException {
		lnkDao = DaoFactory.getInstance().getLnkCommentVideoDao();
		commentDao = DaoFactory.getInstance().getCommentDao();
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String _commentaire =request.getParameter("commentaire");
		long _idvid = Long.parseLong(request.getParameter("idvid"));
		HttpSession session= request.getSession();
		UserSession usersession = (UserSession) session.getAttribute("UserSession");
		String _auteur = usersession.getPseudo();
		long _idUser= usersession.getId();
		
		Comment _comment = new Comment();
		_comment.setAuteur(_auteur);
		_comment.setIdUser(_idUser);
		_comment.setCommentaire(_commentaire);
		_comment.setDate_crea(new Date());
		long last_comment_id = commentDao.addComment(_comment);
		lnkDao.addLink(last_comment_id, _idvid);
		//retour json ajax 
		
		
		
		
	}

}
